/**
 * @Title:  AuthMechanismActivity.java 
 * @Copyright:  XXX Co., Ltd. Copyright YYYY-YYYY,  All rights reserved 
 * @Description:  TODO<请描述此文件是做什么的> 
 * @author:  MeiYi 
 * @data:  2016-1-5 上午11:24:06 
 * @version:  V1.0 
 */

package artid.com.arttogo.activity;

import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.R.integer;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView.HitTestResult;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;
import artid.com.arttogo.R;
import artid.com.arttogo.bean.AddressList;
import artid.com.arttogo.bean.OperationAddress;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.AlertUtils;
import artid.com.arttogo.utils.ToastUtil;

/**
 * 新建收货地址
 * 
 * @author 熊彬
 * @data: 2016-1-5 上午11:24:06
 * @version: V1.0
 */
public class AddAddressActivity extends BaseActivity implements OnClickListener {
    TextView save, textarea;
    EditText name, phone, detail;
    View area;
    String pname, pid, cname, cid;
    AddressList item = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_address);
        mContext = AddAddressActivity.this;
        mBack = (ImageView) findViewById(R.id.back_btn);
        mBack.setOnClickListener(this);
        mTittle = (TextView) findViewById(R.id.tittle_text);
        mTittle.setText("新建收货地址");
        save = (TextView) findViewById(R.id.add_address_save);
        save.setOnClickListener(this);
        name = (EditText) findViewById(R.id.edit_add_name);
        phone = (EditText) findViewById(R.id.edit_add_phone);
        detail = (EditText) findViewById(R.id.edit_add_det);
        area = (View) findViewById(R.id.area_add);
        area.setOnClickListener(this);
        textarea = (TextView) findViewById(R.id.area_text);
        item = (AddressList) getIntent().getSerializableExtra("item");
        if (item != null) {
            mTittle.setText("编辑收货地址");
            name.setText(item.getTrue_name());
            phone.setText(item.getMob_phone());
            detail.setText(item.getAddress());
            textarea.setText(item.getArea_name()+"-"+item.getArea_name2());
            pid = item.getArea_id();
            cid = item.getCity_id();
        }

    }

    void reqData() {
        if (TextUtils.isEmpty(name.getText().toString())) {
            ToastUtil.toast(AddAddressActivity.this, "姓名不能为空");
            return;
        }
        if (TextUtils.isEmpty(phone.getText().toString())) {
            ToastUtil.toast(AddAddressActivity.this, "电话不能为空");
            return;
        }
        if (TextUtils.isEmpty(detail.getText().toString())) {
            ToastUtil.toast(AddAddressActivity.this, "详细地址不能为空");
            return;
        }
        if (TextUtils.isEmpty(textarea.getText().toString())) {
            ToastUtil.toast(AddAddressActivity.this, "请选择所在地区");
            return;
        }
        String addressid = "";
        if (item != null) {
            addressid = item.getAddress_id();
        }
        VolleyBasic.getInstance(AddAddressActivity.this).post(
                Cache.operationAddress,
                VolleyMap.getInstance().operationAddress(
                        name.getText().toString(), phone.getText().toString(),
                        pid, detail.getText().toString(), addressid, cid),
                OperationAddress.class, new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub

                        // List<ArtList> list = (List<ArtList>)vd.getList();
                        // if (list.isEmpty()) {
                        // Config.showTip(MoreCollectionsActivity.this, "无数据");

                        OperationAddress item = (OperationAddress) vd.getData();
                        if (item.getState().equals("1")) {
                            finish();
                        }
                        return;
                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub

                    }
                });
    }

    AlertUtils alert;

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
            case R.id.area_add:

                InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(getWindow().getDecorView()
                            .getWindowToken(), 0);
                }
                alert = new AlertUtils();
                AlertDialog dialog = alert.CityDialog(AddAddressActivity.this,
                        new View.OnClickListener() {

                            @Override
                            public void onClick(View arg0) {
                                // TODO Auto-generated method stub
                                switch (arg0.getId()) {
                                    case R.id.pop_second_level_btnCancel:
                                        alert.dismiss();
                                        break;
                                    case R.id.pop_second_level_btnConfirm:
                                        pname = alert.getProvName();
                                        pid = alert.getPid();
                                        cname = alert.getCityName();
                                        cid = alert.getCid();
                                        textarea.setText(pname + "-" + cname);
                                        alert.dismiss();
                                        break;
                                    default:
                                        break;
                                }
                            }
                        });
                dialog.show();
                break;

            case R.id.add_address_save:
                reqData();
                break;

            case R.id.back_btn:
                finish();
                break;
            default:
                break;
        }
    }

}
